package com.whitehatsec.sentineldataexchange;

import com.whitehatsec.sentineldataexchange.model.WhiteHatVulnerability;
import java.util.*;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author August Detlefsen [augustd@codemagi.ccom]
 * Copyright 2011 WhiteHat Security
 */
public class WhiteHatVulnerabilityListLoader extends DefaultHandler {

	private List<WhiteHatVulnerability> theVulnList = new ArrayList<WhiteHatVulnerability>();
	private String tempVal = null;
	private WhiteHatVulnerability tempVuln = null;

	public List<WhiteHatVulnerability> getVulnerabilityList() {
		return theVulnList;
	}

	// called by the SAXParser at the beginning of every XML element
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		tempVal = "";

		if (qName.equalsIgnoreCase("Vulnerability")) {
			//create a new instance of employee
			tempVuln = new WhiteHatVulnerability();
			tempVuln.setVulnID(attributes.getValue("id"));
			tempVuln.setRequestUrl(attributes.getValue("url"));
			tempVuln.setStatus(attributes.getValue("status"));
			tempVuln.setVulnClass(attributes.getValue("class"));
			tempVuln.setDateFound(attributes.getValue("found"));
			tempVuln.setSiteID(attributes.getValue("site"));
			tempVuln.setSeverity(Integer.parseInt(attributes.getValue("severity").trim()));
			tempVuln.setThreat(Integer.parseInt(attributes.getValue("threat").trim()));
			tempVuln.setScore(Integer.parseInt(attributes.getValue("score").trim()));
			tempVuln.setHRef(attributes.getValue("href"));
			tempVuln.setDescription(attributes.getValue("description"));
			tempVuln.setSolution(attributes.getValue("solution"));
			tempVuln.setAttackVectors(attributes.getValue("attack_vectors"));

		}
	}

	// called by the SAXParser at every chunk of character data
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		tempVal = tempVal + new String(ch, start, length);
	}

	// called by the SAXParser at the end of every XML element
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (qName.equalsIgnoreCase("Vulnerability")) {
			//add it to the list
			theVulnList.add(tempVuln);

			tempVuln = null;
		} else if (qName.equalsIgnoreCase("Description")) {
			tempVuln.setDescription(tempVal);
		} else if (qName.equalsIgnoreCase("Solution")) {
			tempVuln.setSolution(tempVal);
		}
	}
}
